<script>
import {formatTime} from "@/utils/formatTime.js";
import {all, del, add, edit, filter} from "@/api/answer.js";

export default {
  name: "answer",
  data() {
    return {
      addAnswerDialogVisible: false,
      editAnswerDialogVisible: false,
      filterAnswerDialogVisible: false,
      editAnswerFormData: {},
      filterAnswerFormData: {},
      addAnswerFormData: {},
      answerData: []
    }
  },
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.answerData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    addAnswer(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addAnswerDialogVisible = false
          this.addAnswerFormData = {}
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    handleEdit(data) {
      this.editAnswerFormData = data;
      this.editAnswerDialogVisible = true;
    },
    editAnswer(data) {
      edit(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editAnswerDialogVisible = false
          this.editAnswerFormData = {}
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    filterAnswer(data) {
      filter(data).then(res => {
        if (res.code === 200) {
          this.answerData = res.data
          this.filterAnswerDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }
  },
  created() {
    this.refresh();
  }
}
</script>

<template>
  <div id="answer-title">
    论坛回答管理
  </div>
  <div id="answer-box">
    <div id="answer-form">
      <el-button type="primary" @click="filterAnswerDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addAnswerDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>

    <div>
      <el-table id="answer-data" :data="answerData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="问题ID" prop="forumId" width="100"></el-table-column>
        <el-table-column label="回答内容" prop="content" width="250"></el-table-column>
        <el-table-column label="姓名" prop="userName" width="150"></el-table-column>
        <el-table-column label="头像" prop="avatarUrl" width="200">
          <template #default="scope">
            <img :src="scope.row.avatarUrl" style="width: 100px; height: 100px;" alt="头像">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="250">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="250">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
  <!-- 修改表单 -->
  <div>
    <el-dialog title="修改商品" v-model="editAnswerDialogVisible" :close-on-click-modal="false">
      <el-form :model="editAnswerFormData">
        <el-form-item label="内容">
          <el-input v-model="editAnswerFormData.content"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="info" @click="editAnswerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editAnswer(editAnswerFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <!-- 添加表单 -->
  <div>
    <el-dialog title="新增商品" v-model="addAnswerDialogVisible" :close-on-click-modal="false">
      <el-form :model="addAnswerFormData">
        <el-form-item label="用户ID">
          <el-input v-model="addAnswerFormData.userId"></el-input>
        </el-form-item>
        <el-form-item label="问题ID">
          <el-input v-model="addAnswerFormData.forumId"></el-input>
        </el-form-item>
        <el-form-item label="回答内容">
          <el-input v-model="addAnswerFormData.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="addAnswerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addAnswer(addAnswerFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

  <!-- 过滤 -->

  <div>
    <el-dialog title="过滤" v-model="filterAnswerDialogVisible" :close-on-click-modal="false">
      <el-form :model="filterAnswerFormData">
        <el-form-item label="用户名">
          <el-input v-model="filterAnswerFormData.userName"></el-input>
        </el-form-item>
        <el-form-item label="问题ID">
          <el-input v-model="filterAnswerFormData.forumId"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="filterAnswerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="filterAnswer(filterAnswerFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped>
#answer-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#answer-title {
  font-size: 30px;
  margin-left: 20px;
}

#answer-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}

#answer-data {
  align-items: center;
}
</style>